<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 头 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="toAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :props="defaultProps">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="toAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>

                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog

        :title="edit?'编辑部门':'添加部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="dialogVisible"
        width="40%"
        :before-close="handleClose"
        @close="hClose"
      >
        <!-- 将id传个子组件                  v-if="dialogVisible"    父传子 开关思想                                  -->
        <deptDialog
          :id="uerId"
          ref="deptRef"
          :h-list="hList"
          :edit="edit"
          @success="hSuccess"
          @close="dialogVisible=false"
        />
        <!-- <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span> -->
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      list: [], // 储存部门列表
      dialogVisible: false, // 控制弹层的显示和隐藏
      defaultProps: {
        children: 'children',
        label: 'manager'
      },
      uerId: '', // 当前部门的id 作为子部门的父级id
      edit: false, // false 新增  true 编辑
      hList: []
    }
  },
  created() {
    this.hGetDepartments()
  },
  methods: {
    hClose() {
      this.$refs.deptRef.resetForm()
    },
    hSuccess() {
      // 重新渲染列表数据
      this.hGetDepartments()
      // 关闭弹层
      this.dialogVisible = false
    },
    async hGetDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res.data.depts)
        if (res.code !== 10000) return this.$message.error(res.message)
        // 准备业务校验数据
        this.hList = res.data.depts.map(item => {
          const { id, code, name, pid } = item
          return { id, code, name, pid }
        })
        this.$message.success(res.message)
        res.data.depts.shift()
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 是否确认关闭弹层 右上角 x
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 新增
    toAdd(id) {
      this.dialogVisible = true
      // console.log(id)
      this.edit = false
      // 给定义的id赋值
      this.uerId = id
    },
    // 编辑
    hEdit(id) {
      this.dialogVisible = true
      // console.log(id)
      this.edit = true
      this.uerId = id
      // 第二种
      this.$nextTick(() => {
        this.$refs.deptRef.loadDepartments()
      })
    },
    // 删除功能
    hDel(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        if (res.code === 10000) {
          this.$message.success(res.message)
          // 从新渲染数据
          this.hGetDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除11'
        })
      })
    }

  }
}
</script>
